<!-- 审核列表（我发起的、找我的合作） -->
<template>
  <a-card
    style="width: 100%"
    :tab-list="tabList"
    :active-tab-key="isCreate"
    @tabChange="(key) => onTabChange(key)"
    :bordered="false"
    class="br-10"
  >
    <s-table
      ref="table"
      size="default"
      rowKey="id, enabled, title"
      :columns="columns"
      :data="loadData"
      :rowSelection="null"
    >
      <!-- 序号 -->
      <span slot="serial" slot-scope="text, record, index">
        {{ (pagination.current - 1) * pagination.pageSize + parseInt(index) + 1 }}
      </span>

      <!-- 申请合作项目 -->
      <span slot="projectType" slot-scope="text, record">
        {{ cooperationTypeMap[record.cooperationType] }}
        {{ record.projectName }}
      </span>
      <!-- 申请状态 -->
      <template slot="apStatus" slot-scope="text">
        <div>{{ apStatusMap[text] }}</div>
      </template>
      <!-- 申请详情 -->
      <span slot="detail" slot-scope="text, record">
        <a v-has="'business:cooperation:cooperationDetail'" @click="handleToDetail(record)">查看详情</a>
      </span>
      <!-- 操作按钮 -->
      <span slot="action" slot-scope="text, record">
        <template>
          <a
            v-has="'business:cooperation:cooperationApproval:edit'"
            v-if="tenantId == record.toTenantId && record.apStatus === apStatusEnum.DOING"
            @click="handleToDetail(record)"
            >审核</a
          >
          <a
            v-else-if="tenantId == record.createTenantid && record.apStatus === apStatusEnum.DOING"
            @click="handleToDetail(record)"
            >取消申请</a
          >
        </template>
      </span>
    </s-table>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { ApplyList, apStatusMap, cooperationTypeMap } from '@/api/business/cooperation'
import { apStatusEnum } from '@/enums/system'
import Vue from 'vue'
import { TENANTID } from '@/store/mutation-types'
export default {
  components: {
    STable,
  },
  data() {
    return {
      pagination: {
        current: 1,
        pageSize: 10,
      },
      tenantId: Vue.ls.get(TENANTID),
      tabList: [
        {
          key: '1',
          tab: '我发起的',
        },
        {
          key: '2',
          tab: '向我申请的',
        },
      ],
      columns: [
        {
          title: '合作机构',
          dataIndex: 'orgName',
          ellipsis: true,
          width: '150px',
        },
        {
          title: '申请合作项目',
          ellipsis: true,
          width: '150px',
          scopedSlots: { customRender: 'projectType' },
        },
        {
          title: '申请时间',
          dataIndex: 'createTime',
          ellipsis: true,
          width: '150px',
        },
        {
          title: '申请状态',
          dataIndex: 'apStatus',
          width: '150px',
          scopedSlots: { customRender: 'apStatus' },
        },
        {
          title: '申请详情',
          ellipsis: true,
          width: '150px',
          scopedSlots: { customRender: 'detail' },
        },
        {
          title: '操作',
          width: '100px',
          dataIndex: 'action',
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageNum: 1,
      // moduleOptions: [],
      // pageSize: 10,
      // 加载数据方法 必须为 Promise 对象
      loadData: async (parameter) => {
        var params = {
          current: parameter.pageNum,
          size: parameter.pageSize,
        }
        this.listLoading = true
        return ApplyList(Object.assign(params, { isCreate: this.isCreate }))
          .then((res) => {
            this.listLoading = false
            if (res.code === 0) {
              this.pagination.current = parameter.pageNum
              this.pagination.pageSize = parameter.pageSize
              return res
            } else {
              this.$message.error(res.msg)
            }
          })
          .catch((err) => {
            this.listLoading = false
            this.$message.error(err.msg)
          })
      },
      apStatusMap,
      cooperationTypeMap,
      apStatusEnum,
      isCreate: '1',
    }
  },
  methods: {
    onTabChange(key) {
      this.isCreate = key
      this.handleReload()
    },
    handleReload() {
      this.$refs.table.refresh()
    },
    handleToDetail(row) {
      this.$router.push({
        path: '/business/cooperation/cooperationDetail',
        query: {
          data: JSON.stringify(row),
        },
      })
    },
  },
}
</script>
<style scoped lang="less">
.status {
  margin-bottom: 20px;
  .item {
    padding: 8px 15px;
    border: 1px red solid;
    margin-left: 20px;
    cursor: pointer;
    border: 1px #1890ff solid;
    color: #1890ff;
    border-radius: 10px;
  }
  .active {
    background: #1890ff;
    color: #fff;
  }
}
</style>
